$(function () {
    //验证码
    $('#vcode').on('tap', () => {

        $.ajax({
            type: 'GET',
            url: '/user/vCode',
            dataType: 'json',
            success: (result) => {
                $('#number').val(result.vCode)
            },
            complete: () => {
                let count = 60
                $('#vcode').addClass('mui-btn-outlined')
                $('#vcode').attr('disabled', 'disabled')
                let timer = setInterval(() => {
                    $('#vcode').html(count--)
                    if (count <= 0) {
                        $('#vcode').removeClass('mui-btn-outlined')
                        $('#vcode').html('获取验证码')
                        $('#vcode').removeAttr('disabled')
                        clearInterval(timer)
                    }
                }, 1000)

            }
        })
    })

    //注册
    $('#reg').on('tap', () => {
        let account = $.trim($('#account').val())
        let phone = $.trim($('#phone').val())
        let password = $.trim($('#password').val())
        let password_confirm = $.trim($('#password_confirm').val())
        let number = $.trim($('#number').val())
        if (!account) {
            mui.toast('账户不能为空!');
            return
        }
        if (!phone) {
            mui.toast('手机号不能为空!');
            return
        } else if (!/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/.test(phone)) {
            mui.toast('手机号不合法!');
            return
        }
        if (!password) {
            mui.toast('密码不能为空!');
            return
        }
        if (!password_confirm) {
            mui.toast('确认密码不能为空!');
            return
        }
        if (!number) {
            mui.toast('验证码不能为空!');
            return
        }
        if (password !== password_confirm) {
            mui.toast('密码和确认密码不一致!');
            return
        }
        $.ajax({
            type: 'POST',
            url: '/user/register',
            dataType: 'json',
            beforeSend: () => {
                $('#reg').val('注册中...')
            },
            data: {
                username: account,
                password,
                mobile: phone,
                vCode: number
            },
            success: (result) => {
                if (result.error) {
                    mui.toast(result.message);
                    return
                } else if (result.success) {
                    mui.alert('注册成功!', () => {
                        window.location.replace('/login')
                    });

                } else {
                    mui.alert('无法访问服务,请联系wx:13278865643');
                }
            }
        })
    })
})